<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ph="http://java.sun.com/jsf/composite/pharmacy">

    <h:body>

        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">


                <h:form>

                    <p:panel rendered="#{!pharmacyAdjustmentController.printPreview}">
                        <f:facet name="header" >
                            <p:outputLabel value="Department Stock Adjustment : Quantity" />
                            <p:commandButton id="btnzero" value="Go To Zero Adjustment" ajax="false" style="color: red;float: right;"
                                                     action="pharmacy_adjustment_department_by_item_zero" actionListener="#{pharmacyAdjustmentController.newBill()}" ></p:commandButton>
                        </f:facet>
                        <h:panelGrid columns="1" >
                            <p:panel>
                                <h:panelGrid columns="2">
                                    <h:outputLabel value="Category" ></h:outputLabel>
<!--                                    <p:autoComplete  value="#{pharmacyController.category}" 
                                                     forceSelection="true" converter="categoryConverter"
                                                     completeMethod="#{pharmaceuticalItemCategoryController.completeCategory}" 
                                                     var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                                        <p:column headerText="Item">
                                            <h:outputLabel value="#{vt.name}" ></h:outputLabel>
                                        </p:column>
                                        <p:column headerText="Code">
                                            <h:outputLabel value="#{vt.description}" ></h:outputLabel>
                                        </p:column>
                                        <f:ajax event="itemSelect" render="acStock" execute="@this" />
                                    </p:autoComplete>-->
                                    <p:selectOneMenu value="#{pharmacyController.category}" >
                                        <f:selectItem itemLabel="Select Category" />
                                        <f:selectItems value="#{pharmaceuticalItemCategoryController.items}" var="c"
                                                       itemLabel="#{c.name}" itemValue="#{c}" />
                                        <f:ajax event="change" render="acStock" execute="@this" />
                                    </p:selectOneMenu>
                                    <h:outputLabel value="Item" ></h:outputLabel>
                                    <p:autoComplete forceSelection="true" id="acStock" value="#{pharmacyAdjustmentController.item}" 
                                                    completeMethod="#{pharmacyController.completeAllStockItems}" var="i" 
                                                    itemLabel="#{i.name}" itemValue="#{i}" >
                                        <p:column headerText="Item">
                                            <h:outputLabel value="#{i.name}" ></h:outputLabel>
                                        </p:column>
                                        <p:column headerText="Code">
                                            <h:outputLabel value="#{i.code}" ></h:outputLabel>
                                        </p:column>
                                        <f:ajax event="itemSelect" execute="@this" render="det" listener="#{pharmacyAdjustmentController.listnerItemSelect}"/>
                                    </p:autoComplete>
                                    <h:outputLabel value="New Quntity" ></h:outputLabel>
                                    <p:inputText id="input" autocomplete="off" value="#{pharmacyAdjustmentController.qty}" 
                                                 disabled="#{pharmacyAdjustmentController.manualAdjust}" >
                                        <f:ajax event="keyup" execute="@this" render="det" listener="#{pharmacyAdjustmentController.listnerChangeAdjustedStock}"/>
                                    </p:inputText>
                                    <h:outputLabel value="Comment" />
                                    <p:inputTextarea value="#{pharmacyAdjustmentController.comment}" />
                                    <h:outputLabel value="Manual Adjustment" />
                                    <p:selectBooleanCheckbox value="#{pharmacyAdjustmentController.manualAdjust}" >
                                        <f:ajax event="change" execute="@this" render="det input" />
                                    </p:selectBooleanCheckbox>
                                </h:panelGrid>
                                <h:panelGrid columns="2">
                                    <p:commandButton id="btnAdjust" value="Make Adjustments" ajax="false" onclick="if (!confirm('Are you sure you want to Adjust this record?'))
                                                return false;"
                                                     action="#{pharmacyAdjustmentController.adjustDepartmentStockAll}" ></p:commandButton>
                                    <p:commandButton id="btnClear" value="Clear" ajax="false" 
                                                     action="#{pharmacyAdjustmentController.newBill()}" ></p:commandButton>
                                </h:panelGrid>
                            </p:panel>
                            <p:panel>
                                <p:dataTable id="det" value="#{pharmacyAdjustmentController.stocks}" 
                                             var="s" editable="true" >
                                    <p:ajax event="rowEdit" listener="#{pharmacyAdjustmentController.onEdit}" update=":#{p:component('input')}" />
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="Item" />
                                        </f:facet>
                                        <p:outputLabel value="#{s.itemBatch.item.name}" />
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="Code" />
                                        </f:facet>
                                        <p:outputLabel value="#{s.itemBatch.item.code}" />
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="Category" />
                                        </f:facet>
                                        <p:outputLabel value="#{s.itemBatch.item.category.name}" />
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="Batch No" />
                                        </f:facet>
                                        <p:outputLabel value="#{s.itemBatch.batchNo}" />
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="Purcahse Rate" />
                                        </f:facet>
                                        <p:outputLabel value="#{s.itemBatch.purcahseRate}" style="float: right;">
                                            <f:convertNumber pattern="#,##0.00" />
                                        </p:outputLabel>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="Retailsale Rate" />
                                        </f:facet>
                                        <p:outputLabel value="#{s.itemBatch.retailsaleRate}" style="float: right;">
                                            <f:convertNumber pattern="#,##0.00" />
                                        </p:outputLabel>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="Date Of Expiry" />
                                        </f:facet>
                                        <p:outputLabel value="#{s.itemBatch.dateOfExpire}" style="#{commonController.currentDateTime > s.itemBatch.dateOfExpire ?'color: red; ': 
                                                                     commonController.dateAfterThreeMonthsCurrentDateTime > s.itemBatch.dateOfExpire ?'color: red; background-color: yellow;':''}" >
                                            <f:convertDateTime pattern="yyyy MM dd" />
                                        </p:outputLabel>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="Stock" />
                                        </f:facet>
                                        <p:outputLabel value="#{s.stock}" style="float: right;"/>
                                        <f:facet name="footer">
                                            <p:outputLabel value="#{pharmacyAdjustmentController.total}" />
                                        </f:facet>
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <p:outputLabel value="New Qty" />
                                        </f:facet>
                                        <p:cellEditor>
                                            <f:facet name="output" >
                                                <p:outputLabel value="#{s.calculated}" style="float: right;" styleClass="#{s.calculated gt s.stock ? 'greenText':s.calculated lt s.stock ? 'redText':''}" />
                                            </f:facet>
                                            <f:facet name="input" >
                                                <p:inputText value="#{s.calculated}" />
                                            </f:facet>
                                        </p:cellEditor>
                                    </p:column>
                                    <p:column>
                                        <p:rowEditor rendered="#{pharmacyAdjustmentController.manualAdjust}" >
                                        </p:rowEditor>
                                    </p:column>
                                </p:dataTable>
                            </p:panel>
                        </h:panelGrid>
                    </p:panel>

                    <p:panel rendered="#{pharmacyAdjustmentController.printPreview}">
                        <p:commandButton ajax="false" action="#{pharmacyAdjustmentController.newBill()}" value="New Adjustment"/>
                        <p:commandButton ajax="false" value="Print">
                            <p:printer target="print"/>
                        </p:commandButton>
                        <p:panel id="print">
                            <ui:repeat value="#{pharmacyAdjustmentController.bills}" var="b" >
                                <ph:adjustmentBill  bill="#{b}" />
                            </ui:repeat>
                        </p:panel>
                    </p:panel>

                </h:form>

            </ui:define>


        </ui:composition>

    </h:body>
</html>
